package com.zxmzy.scaffold.core.ext

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.annotation.*
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView

/**
 * @author zhangxiaoming
 * @date 2023/7/27
 * @description 资源操作相关
 */

fun Context.color(@ColorRes id: Int) = ContextCompat.getColor(this, id)
fun Context.string(@StringRes id: Int) = resources.getString(id)
fun Context.stringArray(@ArrayRes id: Int) = resources.getStringArray(id)
fun Context.drawable(@DrawableRes id: Int) = ContextCompat.getDrawable(this, id)
fun Context.dimenPx(@DimenRes id: Int) = resources.getDimensionPixelSize(id)
fun Context.integer(@IntegerRes id: Int) = resources.getInteger(id)

fun View.color(@ColorRes id: Int) = context.color(id)
fun View.string(@StringRes id: Int) = context.string(id)
fun View.stringArray(@ArrayRes id: Int) = context.stringArray(id)
fun View.drawable(@DrawableRes id: Int) = context.drawable(id)
fun View.dimenPx(@DimenRes id: Int) = context.dimenPx(id)
fun View.integer(@IntegerRes id: Int) = context.integer(id)

fun Fragment.color(@ColorRes id: Int) = requireContext().color(id)
fun Fragment.string(@StringRes id: Int) = requireContext().string(id)
fun Fragment.stringArray(@ArrayRes id: Int) = requireContext().stringArray(id)
fun Fragment.drawable(@DrawableRes id: Int) = requireContext().drawable(id)
fun Fragment.dimenPx(@DimenRes id: Int) = requireContext().dimenPx(id)
fun Fragment.integer(@IntegerRes id: Int) = requireContext().integer(id)

fun RecyclerView.ViewHolder.color(@ColorRes id: Int) = itemView.color(id)
fun RecyclerView.ViewHolder.string(@StringRes id: Int) = itemView.string(id)
fun RecyclerView.ViewHolder.stringArray(@ArrayRes id: Int) = itemView.stringArray(id)
fun RecyclerView.ViewHolder.drawable(@DrawableRes id: Int) = itemView.drawable(id)
fun RecyclerView.ViewHolder.dimenPx(@DimenRes id: Int) = itemView.dimenPx(id)
fun RecyclerView.ViewHolder.integer(@IntegerRes id: Int) = itemView.integer(id)

inline val Context.layoutInflater: android.view.LayoutInflater
    get() = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as android.view.LayoutInflater